﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Reflection;
using System.IO;
using NGinnBPM.Lib.Schema;
using NGinnBPM.Lib.Data;


namespace NGinnBPM.Lib.Services
{
    internal class XmlPackageInfo
    {
        private string _name;
        private List<string> _processFiles;
        private List<string> _schemaFiles;
        private List<string> _resourceFiles;
        private Dictionary<string, string> _aliases = new Dictionary<string, string>();
        private TypeSet _types = new TypeSet();
        private Dictionary<string, ProcessDefInfo> _procs = new Dictionary<string, ProcessDefInfo>();

        internal class ProcessDefInfo
        {
            public ProcessDefinition ProcessDef;
            public string ProcessFile;
        }

        public static readonly string PACKAGE_NAMESPACE = "http://www.nginn.org/PackageDefinition.1_0";
        
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public void LoadXml(XmlReader rdr)
        {
            XPathDocument pd = new XPathDocument(rdr);
            XPathNavigator n = pd.CreateNavigator();
            n.MoveToFirstChild();
            Load(n);
        }

        public void Load(string fileName)
        {
            using (FileStream fs = new FileStream(fileName, FileMode.Open))
            {
                Load(fs);
            }
        }

        public void Load(Stream stm)
        {
            XmlReaderSettings rs = new XmlReaderSettings();
            using (XmlReader xr = XmlReader.Create(stm, rs))
            {
                XPathDocument xpd = new XPathDocument(xr);
                Load(xpd.CreateNavigator());
            }
        }

        public void Load(XPathNavigator n)
        {
            if (n.NodeType != XPathNodeType.Element)
                n.MoveToFirstChild();
            if (n.LocalName != "package") throw new Exception("Package expected");
            _name = n.GetAttribute("name", "");
            _processFiles = new List<string>();
            _schemaFiles = new List<string>();
            _resourceFiles = new List<string>();
            XPathNodeIterator it = n.SelectChildren(XPathNodeType.Element);
            while (it.MoveNext())
            {
                if (it.Current.Name == "process")
                {
                    _processFiles.Add(it.Current.Value);
                }
                else if (it.Current.Name == "schema")
                {
                    _schemaFiles.Add(it.Current.Value);
                }
                else if (it.Current.Name == "alias")
                {
                    string aliasName = it.Current.GetAttribute("as", "");
                    string procName = it.Current.GetAttribute("process", "");
                    if (aliasName != null && aliasName.Length > 0 && procName != null && procName.Length > 0)
                        _aliases[aliasName] = procName;
                }
                else throw new Exception("Unexpected: " + it.Current.Name);
            }

        }


        public IList<string> ProcessFiles
        {
            get { return _processFiles; }
        }

        public IList<string> SchemaFiles
        {
            get { return _schemaFiles; }
        }

        public IList<string> ResourceFiles
        {
            get { return _resourceFiles; }
        }

        public IDictionary<string, string> Aliases
        {
            get { return _aliases; }
        }

    }
}
